package com.cow.sram.comm.config.tree;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cow.framework.core.base.BaseComponent;
import com.cow.framework.utils.convert.ConvertUtil;
import com.cow.framework.utils.reflection.MapToBeanUtil;
import com.cow.sram.data.entity.Resourceinfo;


/**
 * 树形参数管理
 *
 * @author yangsj 
 **/
@RestController
@EnableAutoConfiguration
@RequestMapping(value="/config/tree")
public class TreeDefController extends BaseComponent{
	@Autowired
	private TreeDefService treeDefService;
	
	/**获取树形定义*/
	@RequestMapping(value="/getAllChildNode")	
	public Object getAllChildNode(@RequestParam String dataType,@RequestParam Integer pid) throws Exception{	
		return getAllChild(dataType,pid);
	}
	
	/**获取树形定义*/	
	private List<Resourceinfo> getAllChild(String dataType,Integer pid){	
		if(pid == null){
			pid = -1;
		}
		List<Resourceinfo> treeList = (List<Resourceinfo>)super.getCache("treeDef", dataType+"-"+ConvertUtil.nvl(pid,""));
		if(treeList != null){
			for(Resourceinfo treeDef:treeList){
				List<Resourceinfo> childList = getAllChild(dataType,treeDef.getResourceid());
				if(childList != null){
					treeDef.setChildList(childList);
				}
			}
		}
		
		return treeList;
	}
	
	/**获取树形定义*/
	@RequestMapping(value="/getChildNode")	
	public Object getChildNode(@RequestParam String dataType,@RequestParam Integer pid) throws Exception{
		if(pid == null){
			pid = -1;
		}
		List<Resourceinfo> treeList = (List<Resourceinfo>)super.getCache("treeDef", dataType+"-"+ConvertUtil.nvl(pid,""));
		List<Resourceinfo> emptyList = new ArrayList<Resourceinfo>();
		if(treeList != null){
			for(Resourceinfo treeDef:treeList){
				if(treeDef.getHasChild() == 1){
					treeDef.setChildList(emptyList);
				}
			}
		}
		return treeList;
	}
	
	/**新增树形节点*/
	@RequestMapping(value="/newTreeNode")	
	public Object newTreeNode(@RequestBody Resourceinfo node) throws Exception{	
		treeDefService.saveTreeDef(node);
		node.setValidFlag(1);
		//更新缓存
		treeDefService.addTreeNodeCache(node);		
		return node;
	}
	
	/**修改树形定义*/
	@RequestMapping(value="/updateTreeNode")	
	public Object updateTreeNode(@RequestBody Map parmMap) throws Exception{	
		Resourceinfo node = treeDefService.getTreeDefById((Integer)parmMap.get("nodeId"));
		MapToBeanUtil.copyMapToBean(parmMap, node);
		treeDefService.saveTreeDef(node);		
		//更新缓存
		treeDefService.updateTreeNodeCache(node);		
		return "success";
	}
	
	/**删除树形定义*/
	@RequestMapping(value="/delTreeNode")	
	public Object delTreeNode(@RequestParam Integer nodeId) throws Exception{	
		Resourceinfo node = treeDefService.getTreeDefById(nodeId);
		//更新缓存
		treeDefService.delTreeNodeCache(node);	
		//置为无效
		node.setValidFlag(0);
		treeDefService.saveTreeDef(node);
		return "success";
	}
}
